var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    "value": "",   // 文本的内容
    "placeholder": "请输入文本",
    "maxlength": 100,  // 最大输入长度，设置为 -1 的时候不限制最大长度
    "focus": true,
    "auto-height": true,  // 是否自动增高，设置auto-height时，style.height不生效
    "adjust-position": true, // 键盘弹起时，是否自动上推页面
    title:[],
    coupon:0,
    username:'',
    telephone:'',
    wholeAddress:'',
    reducePrice:0,
    promotionId:-1,
    groupShoppingId:0,
    couponId:0,
    freight:0,
    remark:"",
    choo_modal:"none",
    addre_modal:"none",
    checked:true,
    radioValue:1,
    onFocus: true,    //textarea焦点是否选中
    isShowText:true, //控制显示 textarea 还是 text
    remark: '',
    index:-1,
    img: 2,//1邮寄  2不邮寄
    giftCardWay:0,
    givenPoint:0,
    ucId:0,
    check:0
  },
  //选择地址
  bindaddress: function () {
    wx.navigateTo({
      url: '/pages/addressList/addressList?userId='+wx.getStorageSync("userId")
      })
  },
  
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
   
    var type = options.type;
    const pages = getCurrentPages()
    const currPage = pages[pages.length - 1]  // 当前页
    //获取手机高度
    this.setData({
      navH: app.globalData.navHeight
    })
    that.setData({
      taskId: options.taskId,
      questionId: options.questionId,
      type: options.type,
      pAndNList: options.pAndNList,
      addrId:options.addrId,
      index:options.index,
      typeShow: options.typeShow,
      groupShoppingId: options.groupShoppingId == undefined ? 0 : options.groupShoppingId,
      yushou: options.yushou,
      consignmenttimestr: options.consignmenttimestr,
      lipinka: options.lipinka,
      shoppingCart: options.shoppingCart,
      communityGroup: options.communityGroup,
      communityheadid: options.communityheadid,
      nickname: options.nickname,
      community: options.community,
    })
    
    console.info("adda" + that.data.typeShow);
    that.setData({
      pAnd: JSON.parse(that.data.pAndNList)
    })
    var pAnd1 = that.data.pAnd;
    console.info(pAnd1);
    var priceCount = 0;
    var point = 0;
    var productId=0;
    for (var i = 0; i < pAnd1.length; ++i) {
      priceCount += pAnd1[i].num * pAnd1[i].price;
      point += pAnd1[i].num * pAnd1[i].point;
      productId = pAnd1[i].productId
    }
    that.setData({
      price: parseFloat(priceCount.toFixed(2)),
      // price2: parseFloat(priceCount.toFixed(2)),
      price3: parseFloat(priceCount.toFixed(2)),
      point: point,
      productId: productId
    })
    wx.loadFontFace({
      family: 'fonts',
      source: 'url("https://test.jwsxmall.com/jiweishengxian/VisbyCF-Bold.otf")',
      success(res) {
        console.log(res.status)
        that.setData({ loaded: true })
      },
      fail: function (res) {
        console.log(res.status)
      },
      complete: function (res) {
        console.log(res.status)
      }
    });
    if(that.data.communityGroup != 1){
    wx.request({
      url: app.globalData.url + "getUserAddressList",
      method: "GET",
      data: {
        userId: wx.getStorageSync("userId")
      },
      success: function (res) {
        var data = res.data.data;
        if (res.data.result == 1) {

          if (data.length > 0) {
            if (currPage.data.wholeAddress == "" && that.data.index == -1) {
              that.setData({
                data: data.length,
                username: data[0].username,
                telephone: data[0].telephone,
                provinceName: data[0].provinceName,
                cityName: data[0].cityName,
                districtName: data[0].districtName,
                streetName: data[0].streetName,
                wholeAddress: data[0].wholeAddress,
                addrId: data[0].addrId,
              })
              console.info("adad1" +data[0].addrId);
              that.addJson(data[0].addrId);
            } else if (that.data.index != -1) {
              console.info(data);
              that.setData({
                data: data.length,
                username: data[that.data.index].username,
                telephone: data[that.data.index].telephone,
                provinceName: data[that.data.index].provinceName,
                cityName: data[that.data.index].cityName,
                districtName: data[that.data.index].districtName,
                streetName: data[that.data.index].streetName,
                wholeAddress: data[that.data.index].wholeAddress,
                addrId: data[that.data.index].addrId,
              })
              that.addJson(that.data.addrId);
            } else {
              that.addJson(that.data.addrId);
            }

          } else {
            that.setData({
              data: 0
            })
            that.addJson(that.data.addrId);
          }

        }
      }
    });
  } else {
    that.setData({
      tuan : 1
    })
  }
   
    var productListJson = "[";
    for (var i = 0; i < that.data.pAnd.length; i++) {
     // list += that.data.pAnd[i].productId+",";
      productListJson+="{productId:"+that.data.pAnd[i].productId+",num:"+that.data.pAnd[i].num+",price:"+that.data.pAnd[i].price+"},"
    }
    productListJson = productListJson.substring(0,productListJson.length-1);
    productListJson+="]";

    wx.request({
      url: app.globalData.url + 'getActivityPromotionByProductId',
      method: "get",
      data: {
        productList: productListJson,
      },
      success: function (res) {
          that.setData({
            promotion: res.data.data,
          })
          if(res.data.data.length > 0){
            that.setData({
              promotionTitle : res.data.data[0].describe,
              promotionId : res.data.data[0].activityId
            })
          }


        var productId = "";
        for (var i = 0; i < that.data.pAnd.length; i++) {
          if (that.data.pAnd[i].specPriceId != undefined) {
            productId += '{"productId":' + that.data.pAnd[i].productId + ',"num":' + that.data.pAnd[i].num + ',"specPriceId":' + that.data.pAnd[i].specPriceId + '},';
          } else {
            productId += '{"productId":' + that.data.pAnd[i].productId + ',"num":' + that.data.pAnd[i].num + '},';
          }

        }
        productId = productId.substr(0, productId.length - 1);
        var addrId = that.data.addrId;
        if (addrId == undefined) {
          addrId = 0;
        }

        var json = '{"addressId" :' + addrId + ',"product":[' + productId + ']}';
        that.setData({
          pAndNList: json
        });
        //判断是否为拼团
        var specPriceId = that.data.specPriceId;
        if (specPriceId == undefined) {
          specPriceId = 0;
        }
        let communityHeadId = that.data.communityheadid;
        if (communityHeadId == undefined || communityHeadId == "undefined") {
          communityHeadId = 0;
        }
        console.info('这是====' + addrId);
        var promotionId = 0;
        if (that.data.type == 2) {
          promotionId = 0;
        } else {
          var activityId = ''
          if (res.data.data.length==0) {
            activityId = 0
          } else {
            activityId = res.data.data[0].activityId;
          }
          promotionId = activityId
        }
        console.info("promotionId=======" + promotionId);
        wx.request({
          url: app.globalData.url + 'countOrderData',
          method: "post",
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          data: {
            userId: wx.getStorageSync("userId"),
            type: that.data.type,
            addressId: addrId,
            promotionId: promotionId,
            productIdAndNumList: "[" + productId + "]",
            couponId: that.data.ucId,
            groupShoppingId: that.data.groupShoppingId,
            specPriceId: specPriceId,
            communityHeadId: communityHeadId
          },
          success: function (res) {
            if (res.data.result == 1) {
              that.setData({
                price2: res.data.data.price,
                point: res.data.data.point,
                freight: res.data.data.expressFee,
                promotionPrice: res.data.data.promotionPrice,
                coupon: res.data.data.couponPrice,
                givenPoint: res.data.data.givenPoint,
                deductPrice: res.data.data.deductPrice,
                deductPoint: res.data.data.deductPoint,
              });
              if (res.data.data.memberDiscountPrice == undefined) {
                that.setData({
                  memberDiscountPrice: 0
                })
              } else {
                that.setData({
                  memberDiscountPrice: res.data.data.memberDiscountPrice,
                })
              }
            } else {
              wx.showToast({
                title: res.data.errmsg,
                icon: "none"
              })
            }
          }
        })
      }
    })

   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var that = this;
    
    
    //判断是否为拼团
    //that.getFinalInfo();
    that.addJson(that.data.addrId);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (options) {
    var that = this;
    wx.request({
      url: app.globalData.url + "getUserByWechatUnoinId",
      method: "GET",
      data: {
        wechatUnoinId: wx.getStorageSync("wechatUnoinId"),
        token: that.data.token,
        phoneModel: app.globalData.modelmes
      },
      success: function (res) {
        var data = res.data;
        if (data.result==1){
          wx.setStorageSync("balance", data.data.balance.toFixed(2));
          wx.setStorageSync("points", data.data.points);
        }
      }
    });
    that.setData({
      balance: wx.getStorageSync("balance")
    })
    const pages = getCurrentPages()
    const currPage = pages[pages.length - 1]  // 当前页
    console.info("促销的"+that.data.promotionId);
    console.info("促销的" + that.data.promotionTitle);
    if(that.data.promotionId == 0){
      that.setData({
        promotionId:0,
        promotionTitle:''
      })
    }
    // if (that.data.typedianji==1) {
    //   that.setData({
    //     price2: parseFloat((that.data.price - that.data.reducePrice).toFixed(2)) < 0 ? 0.01 : parseFloat((that.data.price - that.data.reducePrice).toFixed(2))
    //   })
    // } else {
    //     that.setData({
    //       price2: that.data.price2
    //     })
    // }
   
    if (wx.getStorageSync("points") == 0) {
      that.setData({
        points: 0
      })
    } else {
      that.setData({
        points: wx.getStorageSync("points")
      })
    }
    var productIdList = [];
    var productNumList = [];
    var productPriceList = [];
    for (var i = 0; i < that.data.pAnd.length; i++) {
      var totalPrice = that.data.pAnd[i].num * that.data.pAnd[i].price;
      productIdList[i] = "{productId:" + that.data.pAnd[i].productId + ",totalPrice:" + that.data.pAnd[i].price + "}"
      productNumList[i] = that.data.pAnd[i].num
      productPriceList[i] = that.data.pAnd[i].price
    }
    var str = "[";
    var str1 = "";
    var str2 = "";
    for (var i = 0; i < productIdList.length; i++) {
      str += productIdList[i] + ",";
      str1 += productNumList[i] + ",";
      str2 += productPriceList[i] + ",";
    }
    str = str.substring(0, str.length - 1);
    str1 = str1.substring(0, str1.length - 1);
    str2 = str2.substring(0, str2.length - 1);
    str += "]";
    wx.request({
      url: app.globalData.url + 'getUserConponListByUserId',
      method: 'GET',
      data: {
        userId: wx.getStorageSync("userId"),
        type: 1,
        threshold: that.data.price,
        productIdList: str,
        productNumList: str1,
        productPriceList: str2
      },
      success: function (res) {
        that.setData({
          couponsList: res.data.data
        });
        wx.request({
          url: app.globalData.url + 'userAvailableAndUnavailableCoupon',
          method: 'GET',
          data: {
            userId: wx.getStorageSync("userId"),
            type: 1,
            threshold: that.data.price,
            productIdList: str,
            productNumList: str1,
            productPriceList: str2
          },
          success: function (res) {
            couponsList: res.data.data
            var num=0;
            for (var i = 0; i < res.data.data.length;i++){
              if (res.data.data[i].available==true) {
                  num++;
                }
            }
            that.setData({
              num:num
            })
          },
        });
      },
    });

    console.info(currPage);
    if (that.data.promotionTitle != undefined) {
      that.getFinalInfo();
      that.addJson(that.data.addrId);
    }
    if (that.data.reducePrice != undefined) {
      that.getFinalInfo();
      that.addJson(that.data.addrId);
    }
  },

  //每次修改该订单都要调用该方法1
  getFinalInfo:function(){
    var that = this;
    console.info(this.data.promotionId+"DDD");
    if (that.data.promotionId ==undefined) {
        that.setData({
          promotionId:0
        })
    }
    var productId = "";
    for (var i = 0; i < that.data.pAnd.length; i++) {
      if(that.data.pAnd[i].specPriceId!=undefined){
        productId += '{"productId":' + that.data.pAnd[i].productId + ',"num":' + that.data.pAnd[i].num +',"specPriceId":' + that.data.pAnd[i].specPriceId + '},';
      }else{
        productId += '{"productId":' + that.data.pAnd[i].productId + ',"num":' + that.data.pAnd[i].num + '},';
      }
    }
    productId = productId.substr(0, productId.length - 1);
    var addrId = that.data.addrId;
    if (addrId == undefined) {
      addrId = 0;
    }
    if (that.data.couponId == undefined) {
     that.setData({
       couponId:0
     })
    }
    var json = '{"addressId" :' + addrId + ',"product":[' + productId + ']}';
    that.setData({
      pAndNList:json
    });
    var specPriceId = that.data.specPriceId;
    if(specPriceId == undefined){
      specPriceId = 0;
    }
    var promotionId=0;
     if (that.data.type == 2) {
      promotionId = 0;
     } else {
      promotionId = that.data.promotionId
     }
    console.info(that.data.promotionId+"=====");
    var addrid = that.data.addrId;
    if (that.data.addrId == undefined) {
      addrid = 0;
     }
    wx.request({
      url: app.globalData.url + 'countOrderData',
      method: "post",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        userId: wx.getStorageSync("userId"),
        type: that.data.type,
        addressId: addrid,
        promotionId: promotionId,
        productIdAndNumList: "[" + productId + "]",
        couponId: that.data.ucId,
        groupShoppingId: that.data.groupShoppingId,
        specPriceId:specPriceId
      },
      success: function (res) {
        if (res.data.result == 1) {
          that.setData({
            price2: res.data.data.price,
            point: res.data.data.point,
            freight: res.data.data.expressFee,
            promotionPrice: res.data.data.promotionPrice,
            coupon: res.data.data.couponPrice,
            givenPoint: res.data.data.givenPoint,
            promotionMsg: res.data.data.promotionMsg,
          });
          if (res.data.data.memberDiscountPrice == undefined) {
            that.setData({
              memberDiscountPrice: 0
            })
          } else {
            that.setData({
              memberDiscountPrice: res.data.data.memberDiscountPrice,
            })
          }
        }
      }
    })
  },

  addtion: function(e){
    var that = this;
    var index = e.currentTarget.dataset.index; // 获取data- 传进来的index
    var arr = that.data.pAnd;
    console.info(that.data.pAnd);
    if (arr[index].num < arr[index].inventoryQuantity) {
      if (arr[index].seckillNum > 0) {
        if (arr[index].num < arr[index].seckillNum) {
          arr[index].num++;
        } else {
          wx.showToast({
            title: '达到限购数量',
            icon: "none"
          })
        }
      } else if (arr[index].groupShoppingLimitNum > 0) {
        if (arr[index].num < arr[index].groupShoppingLimitNum) {
          arr[index].num++;
        } else {
          wx.showToast({
            title: '达到限购数量',
            icon: "none"
          })
        }
      } else {
        arr[index].num++;
        that.arr();
      }

    }else{
      wx.showToast({
        title: '库存不足',
        icon: "none"
      })
    }
    
    that.setData({
      pAnd: arr
    });
    var priceCount = 0;
    var point = 0;
    for (var i = 0; i < arr.length; ++i) {
      priceCount += arr[i].num * arr[i].price;
      point += arr[i].num * arr[i].point;
    }
    that.setData({
      price: parseFloat(priceCount.toFixed(2)),
      price2: parseFloat(priceCount.toFixed(2)),
      price3: parseFloat(priceCount.toFixed(2)),
      point: point
    })
    //判断是否为拼团
    that.getFinalInfo();
    // console.info(that.data.addressList[e.currentTarget.dataset.index].addrId);
    wx.request({
      url: app.globalData.url + "getTotalFreight",
      data: {
        json: "{'addressId':" + that.data.addrId + ",'product':[{'productId':" + that.data.productId + ",'num':1}]}",
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        that.setData({
          freight: parseFloat(res.data.data.toFixed(2)),
          index: e.currentTarget.dataset.index
        })
      },
    });
  },

  subtraction: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index; // 获取data- 传进来的index
    var arr = that.data.pAnd;
    if (arr[index].num > 1){
      arr[index].num--;
      that.setData({
        pAnd: arr
      });
      var priceCount = 0;
      var point = 0;
      for (var i = 0; i < arr.length; ++i) {
        priceCount += arr[i].num * arr[i].price;
        point += arr[i].num * arr[i].point;
      }
      that.setData({
        price: parseFloat(priceCount.toFixed(2)),
        price2: parseFloat(priceCount.toFixed(2)),
        price3: parseFloat(priceCount.toFixed(2)),
        point: point
      })
      //判断是否为拼团
        that.getFinalInfo();
        that.arr();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  addJson: function (addrId) {
    var that = this;
    var productId = "";
    for (var i = 0; i < that.data.pAnd.length; i++) {
      if(that.data.pAnd[i].specPriceId!=undefined){
        productId += '{"productId":' + that.data.pAnd[i].productId + ',"num":' + that.data.pAnd[i].num +',"specPriceId":' + that.data.pAnd[i].specPriceId + '},';
      }else{
        productId += '{"productId":' + that.data.pAnd[i].productId + ',"num":' + that.data.pAnd[i].num + '},';
      }
    }
    productId = productId.substr(0, productId.length - 1);
    var addrId = that.data.addrId
    if (addrId==undefined || addrId=='undefined' ) {
      addrId=0;
    }
    var json = '{"addressId" :' + addrId + ',"product":[' + productId+']}';
    if (addrId != 0) {
      wx.request({
        url: app.globalData.url + "getTotalFreight",
        method: "GET",
        data: {
          json: JSON.parse(json)
        },
        success: function (res) {
          console.info(res);
          var data = res.data.data;
          if (res.data.result == 1) {
             freight: parseFloat(data.toFixed(2));
              if (that.data.reducePrice==0) {
                // price1: (that.data.price1 - data).toFixed(2)
                that.setData({
                  freight: parseFloat(data.toFixed(2)),
                  // price2: parseFloat((that.data.price2 - data).toFixed(2))
                })
            }
            
          }else{
            if(addrId != 0){
              that.modal_ad();
              that.setData({
                thisFreight:'不在区域',
              })
            }
           
            
          }
        }
      });
    }
    
  },
  bindSubmitOrder:function(){
    var that = this;
    var promotionId = that.data.promotionId;
    if (that.data.tuan == 1) {
      wx.showToast({
        title: "请添加收货地址",
        icon: 'none'
      })
      return;
    }
    if(that.data.promotionId == -1){
      promotionId = 0;
    }
    var userId = wx.getStorageSync("userId");
    var points = wx.getStorageSync("points");
    if (that.data.taskId == undefined) {
      that.setData({
        taskId:0
      })
    }
    if (that.data.questionId == undefined) {
      that.setData({
        questionId: 0
      })
    }
    console.info("收货地址" + that.data.addrId );
    if (that.data.addrId == undefined || that.data.addrId == 0 && that.data.communityGroup !=1) {
      wx.showToast({
        title: "请添加收货地址",
        icon: 'none'
      })
      return;
    }
    if(that.data.thisFreight == "不在区域"&&that.data.giftCardWay == 1){
      that.modal_ad();
      return;
    }
    if (that.data.groupShoppingId==undefined) {
      that.setData({
        groupShoppingId:0
      })
    }
    if (that.data.pAndNList!=undefined) {
        var type = that.data.type;
         if (type == 6) {
          type = 6
        } else if (that.data.communityGroup == 1) {
          type = 3
         } else if (type == undefined) {
           type = 1
         } else if (that.data.radioValue == 2)  {
          var banlance = true;
        } else {
          var banlance = false;
        }
      
     
      var list = []
      
      for (var i = 0; i < that.data.pAnd.length; i++){
        if (that.data.pAnd[i].seckillNum == undefined) {
          var seckillNum =0
        } else {
          var seckillNum = that.data.pAnd[i].seckillNum
        }
        if (that.data.pAnd[i].inventoryQuantity == undefined) {
          var inventoryQuantity = 0
        } else {
          var inventoryQuantity = that.data.pAnd[i].inventoryQuantity
        }
        if(that.data.pAnd[i].specPriceId!=undefined){
          var plist = '{"productId":"' + that.data.pAnd[i].productId + '","num":' + that.data.pAnd[i].num + ',"price":' + that.data.price2 + ',"title":"' + that.data.pAnd[i].title + '","thumbnailImage":"' + that.data.pAnd[i].thumbnailImage + '","point":' + that.data.pAnd[i].point + ',"seckillNum":' + seckillNum + ',"inventoryQuantity":' + inventoryQuantity + ',"specPriceId":' + that.data.pAnd[i].specPriceId + '}';
        }else{
          var plist = '{"productId":"' + that.data.pAnd[i].productId + '","num":' + that.data.pAnd[i].num + ',"price":' + that.data.price2 + ',"title":"' + that.data.pAnd[i].title + '","thumbnailImage":"' + that.data.pAnd[i].thumbnailImage + '","point":' + that.data.pAnd[i].point + ',"seckillNum":' + seckillNum + ',"inventoryQuantity":' + inventoryQuantity +  '}';
        }
        
        console.info(plist);
        list.push(JSON.parse(plist));
      }
      console.info(list);
      console.info(JSON.stringify(list) );
      console.info(that.data.addrId);
      console.info(that.data.promotionMsg);
      var promotionMsg = '';
      if (that.data.promotionMsg != undefined) {
        promotionMsg = that.data.promotionMsg
      }
      if (promotionId != 0 && promotionMsg != '') {
        wx.showModal({
          title: '提示',
          content: that.data.promotionMsg,
          success: function (sm) {
            if (sm.confirm) {
              wx.navigateTo({
                url: '/pages/cashier/cashier?productIdAndNumList=' + JSON.stringify(list) + '&addressId=' + that.data.addrId + '&type=' + type + '&couponId=' + that.data.ucId + '&remark=' + that.data.remark + '&banlance=' + banlance + '&groupShoppingId=' + that.data.groupShoppingId + '&giftCardWay=' + that.data.giftCardWay + "&price1=" + that.data.price2 + '&shoppingCart=' + that.data.shoppingCart + '&communityGroup=' + that.data.communityGroup + '&communityheadid=' + that.data.communityheadid + '&typeShow=' + that.data.typeShow + '&promotionId=' + promotionId + '&deduct=' + that.data.check
              })
            } else {
              return;
            }
          }
        });
      } else {
        console.info(87878);
        wx.navigateTo({
          url: '/pages/cashier/cashier?productIdAndNumList=' + JSON.stringify(list) + '&addressId=' + that.data.addrId + '&type=' + type + '&couponId=' + that.data.ucId + '&remark=' + that.data.remark + '&banlance=' + banlance + '&groupShoppingId=' + that.data.groupShoppingId + '&giftCardWay=' + that.data.giftCardWay + "&price1=" + that.data.price2 + '&shoppingCart=' + that.data.shoppingCart + '&communityGroup=' + that.data.communityGroup + '&communityheadid=' + that.data.communityheadid + '&typeShow=' + that.data.typeShow + '&promotionId=' + promotionId + '&deduct=' + that.data.check
        })
      }

    }
  },
  youhui:function(){
    var that = this;
    console.info(that.data.price);
    wx.navigateTo({
      url: '/pages/chooseCoupons/chooseCoupons?price=' + that.data.price + "&productList=" +JSON.stringify(that.data.pAnd)
    })
  },
  onShowTextare() {       //显示textare
    this.setData({
      isShowText: false,
      onFacus: true
    })
  },
  onShowText() {       //显示text
    this.setData({
      isShowText: true,
      onFacus: true
    })
  },
  onRemarkInput(event) {               //保存输入框填写内容
    var value = event.detail.value;
    this.setData({
      remark: value,
    });
  },
  // 弹出
  modal_s: function (e) {
    this.setData({
      choo_modal: "block",
      //z:true
    });
  },
  // 弹出
  modal_ad: function (e) {
    this.setData({
      addre_modal: "block",
      //z:true
    });
  },
  //消失
  modal_n: function () {
    this.setData({
      choo_modal: "none",
      //z:false
    });
  },
  modal_ad1: function () {
    this.setData({
      addre_modal: "none",
      //z:false
    });
  },
  radio: function (e) {
    var that = this;
    var radioValue = e.currentTarget.dataset.value;
    that.setData({
      radioValue: radioValue
    })
    console.info(radioValue);
    if (radioValue == 1) {
      that.setData({
        checked1:false,
        checked:true
      })
    }
    if (radioValue == 2) {
      that.setData({
        checked1: true,
        checked: false
      })
    }
  },

  toPromotion:function(e){
    var that = this;
    var list = [];
    var priceList = [];
    var num = [];
      for (var i = 0; i < that.data.pAnd.length; i++) {
      list.push(that.data.pAnd[i].productId);
      priceList.push(that.data.pAnd[i].price);
      num.push(that.data.pAnd[i].num);
    }
    var priceList1 = "";
    for (var i = 0; i < priceList.length; i++) {
      priceList1 += priceList[i]+",";
    }
    priceList1 = priceList1.substring(0, priceList1.length - 1);
    var numList = "";
    for (var i = 0; i < num.length; i++) {
      numList += num[i]+",";
    }
    numList = numList.substring(0, numList.length - 1);
    wx.navigateTo({
      url: '/pages/promotion/promotion?productList=' + list + '&promotionId=' + that.data.promotionId+ '&priceList=' + priceList1+ '&numList=' + numList,
    })
  },
  navBack: function () {
    wx.navigateBack({
      data:1
    })
  },
  note: function (e) {
    var that = this;
    that.setData({
      remark: e.detail.value
    })
  }, 
  // 弹出
  trueMail: function (e) {
    this.setData({
      choo_modal: "block",
      //z:true
    });
  },
  //消失
  modal_n: function () {
    this.setData({
      choo_modal: "none",
      //z:false
    });
  },
  chooseThis: function (e) {
    if (e.currentTarget.dataset.index==1){
      this.setData({
        giftCardWay: 1,
      });
    }else{
      this.setData({
        giftCardWay: 0,
      });
    }
    this.setData({
      img: e.currentTarget.dataset.index,
    });
  },
  rules:function(){
    wx.navigateTo({
      url: '/pages/webView/webView?url=' + app.globalData.url + 'getWebPage&webId=groupShoopingRules',
    });
  },
  checkboxChange(e){
    var that = this;
    if (e.detail.value == '' || e.detail.value == undefined ) {
      that.setData({
        check:0
      })
    }else {
      that.setData({
        check: 1
      })
    }
    var specPriceId = that.data.specPriceId;
    if (specPriceId == undefined) {
      specPriceId = 0;
    }
    var productId = "";
    for (var i = 0; i < that.data.pAnd.length; i++) {
      if (that.data.pAnd[i].specPriceId != undefined) {
        productId += '{"productId":' + that.data.pAnd[i].productId + ',"num":' + that.data.pAnd[i].num + ',"specPriceId":' + that.data.pAnd[i].specPriceId + '},';
      } else {
        productId += '{"productId":' + that.data.pAnd[i].productId + ',"num":' + that.data.pAnd[i].num + '},';
      }
    }
    productId = productId.substr(0, productId.length - 1);
    var promotionId=0;
    if (that.data.type == 2) {
     promotionId = 0;
    } else {
     promotionId = that.data.promotionId
    }
    wx.request({
      url: app.globalData.url + 'countOrderData',
      method: "post",
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        userId: wx.getStorageSync("userId"),
        type: that.data.type,
        addressId: that.data.addrId,
        promotionId: promotionId,
        productIdAndNumList: "[" + productId + "]",
        couponId: that.data.ucId,
        groupShoppingId: that.data.groupShoppingId,
        specPriceId: specPriceId,
        deduct: that.data.check
      },
      success: function (res) {
        if (res.data.result == 1) {
          that.setData({
            price2: res.data.data.price,
            point: res.data.data.point,
            freight: res.data.data.expressFee,
            promotionPrice: res.data.data.promotionPrice,
            coupon: res.data.data.couponPrice,
            givenPoint: res.data.data.givenPoint,
            deductPrice: res.data.data.deductPrice,
            deductPoint: res.data.data.deductPoint,
          });
          if (res.data.data.memberDiscountPrice == undefined) {
            that.setData({
              memberDiscountPrice: 0
            })
          } else {
            that.setData({
              memberDiscountPrice: res.data.data.memberDiscountPrice,
            })
          }
        }
      }
    })
  },
  arr:function () {
    var that = this;
    var productListJson = "[";
    for (var i = 0; i < that.data.pAnd.length; i++) {
      // list += that.data.pAnd[i].productId+",";
      productListJson += "{productId:" + that.data.pAnd[i].productId + ",num:" + that.data.pAnd[i].num + ",price:" + that.data.pAnd[i].price + "},"
    }
    productListJson = productListJson.substring(0, productListJson.length - 1);
    productListJson += "]";

    wx.request({
      url: app.globalData.url + 'getActivityPromotionByProductId',
      method: "get",
      data: {
        productList: productListJson,
      },
      success: function (res) {
        that.setData({
          promotion: res.data.data,
        })
        if (res.data.data.length > 0) {
          that.setData({
            promotionTitle: res.data.data[0].describe,
            promotionId: res.data.data[0].activityId
          })
        }


        var productId = "";
        for (var i = 0; i < that.data.pAnd.length; i++) {
          if (that.data.pAnd[i].specPriceId != undefined) {
            productId += '{"productId":' + that.data.pAnd[i].productId + ',"num":' + that.data.pAnd[i].num + ',"specPriceId":' + that.data.pAnd[i].specPriceId + '},';
          } else {
            productId += '{"productId":' + that.data.pAnd[i].productId + ',"num":' + that.data.pAnd[i].num + '},';
          }

        }
        productId = productId.substr(0, productId.length - 1);
        var addrId = that.data.addrId;
        if (addrId == undefined) {
          addrId = 0;
        }

        var json = '{"addressId" :' + addrId + ',"product":[' + productId + ']}';
        that.setData({
          pAndNList: json
        });
        //判断是否为拼团
        var specPriceId = that.data.specPriceId;
        if (specPriceId == undefined) {
          specPriceId = 0;
        }
        let communityHeadId = that.data.communityheadid;
        if (communityHeadId == undefined || communityHeadId == "undefined") {
          communityHeadId = 0;
        }
        console.info('这是====' + addrId);
        var promotionId = 0;
        if (that.data.type == 2) {
          promotionId = 0;
        } else {
          var activityId = ''
          if (res.data.data.length == 0) {
            activityId = 0
          } else {
            activityId = res.data.data[0].activityId;
          }
          promotionId = activityId
        }
        console.info("promotionId=======" + promotionId);
        wx.request({
          url: app.globalData.url + 'countOrderData',
          method: "post",
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          data: {
            userId: wx.getStorageSync("userId"),
            type: that.data.type,
            addressId: addrId,
            promotionId: promotionId,
            productIdAndNumList: "[" + productId + "]",
            couponId: that.data.ucId,
            groupShoppingId: that.data.groupShoppingId,
            specPriceId: specPriceId,
            communityHeadId: communityHeadId
          },
          success: function (res) {
            if (res.data.result == 1) {
              that.setData({
                price2: res.data.data.price,
                point: res.data.data.point,
                freight: res.data.data.expressFee,
                promotionPrice: res.data.data.promotionPrice,
                coupon: res.data.data.couponPrice,
                givenPoint: res.data.data.givenPoint,
                deductPrice: res.data.data.deductPrice,
                deductPoint: res.data.data.deductPoint,
              });
              if (res.data.data.memberDiscountPrice == undefined) {
                that.setData({
                  memberDiscountPrice: 0
                })
              } else {
                that.setData({
                  memberDiscountPrice: res.data.data.memberDiscountPrice,
                })
              }
            } else {
              wx.showToast({
                title: res.data.errmsg,
                icon: "none"
              })
            }
          }
        })
      }
    })
  }
})